﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Data.SqlClient;
using Perseus.WCF.Sequence;
using Perseus.WCF.REST.Contract.Data;
using Perseus.WCF.REST.Contract.Service;

namespace Perseus.WCF.REST.Service
{
    public class EmployeeService : IEmployeeService
    {
        public List<Employee> GetAllEmpByXml()
        {
            return GetAllEmployees();
        }

        public List<Employee> GetAllEmpByJson()
        {
            return GetAllEmployees();
        }

        public DbResponse UpdateEmployee(string emp_id, string firstName, string lastName, string birthDate)
        {
            IBoolFlow flow = new UpdateEmpFlow();
            Employee employee = new Employee(firstName, lastName, Convert.ToDateTime(birthDate));
            ((UpdateEmpFlow)flow).EmpId = emp_id;
            ((UpdateEmpFlow)flow).Employee = employee;

            DbResponse response;
            if (flow.Go())
            {
                response = new DbResponse
                {
                    Result = "Success",
                    Success = true,
                    ErrorMessage = ""
                };
            }
            else
            {
                response = new DbResponse
                {
                    Result = "Fail",
                    Success = false,
                    ErrorMessage = "Update Employee Error"
                };
            }

            return response;
        }

        private List<Employee> GetAllEmployees()
        {
            IListFlow<Employee> flow = new GetAllEmpFlow();
            IList<Employee> empList = ((GetAllEmpFlow)flow).GoList();

            return (List<Employee>)empList;
        }
    }
}
